<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <PageTools :show-before="true">
        <span slot="before"> 共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="danger">简单表头导出</el-button>
          <el-button size="small" type="info">复杂表头导出</el-button>
          <el-button size="small" type="success">excel导入</el-button>
          <el-button size="small" type="primary" @click="showDialog = true">新增员工</el-button>
        </template>
      </pagetools>
      <el-card>
        <el-table :data="tableDataList" algin="center">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column :formatter="formatEmployment" label="聘用形式" sortable="" prop="formOfEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <!-- 传入插槽 -->
            <template v-slot="{row}">
              {{ dayJs(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState" />
          <el-table-column label="操作" fixed="right" width="200">
            <template v-slot="{row}">
              <el-button size="small" type="text">查看</el-button>
              <el-button size="small" type="text">转正</el-button>
              <el-button size="small" type="text">调岗</el-button>
              <el-button size="small" type="text">角色</el-button>
              <el-button size="small" type="text" @click="deleteEmployee(row.id)">删除</el-button>
            </template>

          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height:80px">
          <el-pagination
            :page-size="page.size"
            layout="prev,pager,next"
            background
            :total="page.total"
            @current-change="changePage"
          />
        </el-row>

      </el-card>
    </div>
    <addEmployee :show-dialog.sync="showDialog" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
// 引入枚举对象
import EmployeeEnum from '@/api/constant/employees'
import addEmployee from './components/add-employee.vue'

export default {
  components: {
    addEmployee
  },
  data() {
    return {
      EmployeeEnum,
      page: {
        size: 10,
        page: 1,
        total: 0
      },
      tableDataList: [],
      loading: false,
      showDialog: false
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      this.loading = true
      // rows就是当前页 整页的数据
      const { total, rows } = await getEmployeeList(this.page)
      this.page.total = total
      this.tableDataList = rows
      await new Promise(resolve => setTimeout(() => resolve(), 100))
      this.loading = false
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getEmployeeList()
    },
    // 格式化聘用形式
    formatEmployment(row, column, cellValue, index) {
    //  找满足条件的
      const obj = this.EmployeeEnum.hireType.find(item => item.id + '' === cellValue + '')
      return obj ? obj.value : '未知'
    },
    dayJs(time) {
      var dayjs = require('dayjs')
      return dayjs(time).format('YYYY-MM-DD')
    },
    async deleteEmployee(id) {
      try {
        await this.$confirm('您确定删除该员工吗')
        await delEmployee(id)
        this.getEmployeeList()
        this.$message.success('删除员工成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
